package com.xxl.job.executor.sample.frameless.test.wait_notifyDemo;

public class Demo1 {

    final static Object obj = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
           synchronized (obj) {
               System.out.println("t1执行。。。");
               try {
                   // 让线程在obj上一会等待下去
                   obj.wait();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("t1其它代码。。。");
           }
        },"t1").start();
        new Thread(() -> {
            synchronized (obj) {
                System.out.println("t2执行。。。");
                try {
                    // 让线程在obj上一会等待下去
                    obj.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t2其它代码。。。");
            }
        },"t2").start();
        // 主线程两秒后执行
        Thread.sleep(2000);
        System.out.println("唤醒obj上其它线程");
        synchronized (obj) {
            // 唤醒obj上一个线程
            //obj.notify();
            // 唤醒obj上所有线程
            obj.notifyAll();
        }
    }
}
